<?php
class Users extends Zend_Db_Table_Abstract
{
	protected $_name = 'users';
	protected $_primary = 'id';

	public static function login($username=null, $userPassword=null)
    {
        
        $returnValue = false;
        /*
         * Validate the email address
         */
        $emailValidator = new Zend_Validate_EmailAddress();
        
        /*
         * Email Address is invalid. Notify the user.
         */
        if (!$emailValidator->isValid($username)) {
            return false;
        } // if (!$validator->isValid($username))
                
        /*
         * setup authentication
         */
        $authAdapter  = new Zend_Auth_Adapter_DbTable($db);
        $authAdapter->setTableName('member')
                    ->setIdentityColumn('username')
                    ->setCredentialColumn('userPassword')
                    ->setIdentity($username)
                    ->setCredential($userPassword);
        
        /*
         * authenticate
         */
        $result = $authAdapter->authenticate();

        if ($result->isValid()) {
            $memberArray = $authAdapter->getResultRowObject('id');
            $returnValue = (int)$memberArray->id;
        } // if ($result->isValid())
        
        return $returnValue;
    } // public static function login($username=null, $userPassword=null)
        
    public function registerUser($formdata)
    {
    	$date_registred = time();
    	//INFO:noter l'utilité de la fonction htmlUtf8
    	$userdata = array(		'username'=>$formdata['username'],
            					'password'=>sha1(Zend_registry::get('config')->auth->salt.$formdata['password']),
            					'firstname'=>Wmp_Lib::htmlUtf8($formdata['firstname']),
            					'lastname'=>Wmp_Lib::htmlUtf8($formdata['lastname']),
            					'email'=>$formdata['email'],
    							'date_created'=>$date_registred,
    							'date_updated'=>$date_registred
            				);
    	return $this->insert($userdata);
    }
    
    /*
     * return true if the email address is in the database, false if not or error.
     */
    public static function  emailCheck($emailAddress)
    {
        /*
         * Validate the email address
         */
        $emailValidator = new Zend_Validate_EmailAddress();
        
        /*
         * Email Address is invalid. Notify the user.
         */
        if (!$emailValidator->isValid($emailAddress)) {
                return false;
        } // if (!$validator->isValid($emailAddress))
        
        $db = Zend_registry::get('db');                
        $sql = 'SELECT id FROM member WHERE emailAddress = ?';        
        $result = $db->fetchOne($sql, array($emailAddress));
        
        return (int)$result;
        
    } // public static function  emailCheck($emailAddress)    

} // class User
